/**
 * Created by mengxiaxia on 2017/1/15.
 */
(function () {
    var app=angular.module("routingDemoApp",['ngRoute']);

    app.config([
        '$logProvider','$routeProvider',function (log,route) {
            log.debugEnabled(true);
            route
                .when('/',{
                controller:'HomeController',
                controllerAs:'home',
                //template:'<button class="btn btn-danger" ng-click="home.refresh()">刷新（刷新当前路由）</button><button class="btn btn-danger" ng-click="home.reload()">刷新（刷新当前页面）</button><h2>当前路由:{{currentRoute}}</h2><H1>This is an inline template</H1><H1>{{message}}</H1>'
                templateUrl:'12.1.route.template.html'
            })
                .when('/schools',{
                    controller:'AllSchoolsController',
                    controllerAs:'schools',
                    // template:'<H1>{{message}}</H1>',
                    templateUrl:'12.1.route.template.html',
                    caseInsensitiveMatch: true/** 禁止大小写敏感 **/
                })
                .when('/classrooms',{
                    controller:'AllClassroomsController',
                    controllerAs:'classrooms',
                    // template:'<H1>{{message}}</H1>'
                    templateUrl:'12.1.route.template.classroom.html'
                })
                .when('/activities',{
                    controller: 'AllActivitiesController',
                    controllerAs: 'activities',
                    // template:'<H1>This is an inline template</H1><H1>{{message}}</H1>'
                    templateUrl:'12.1.route.template.html'
                })
                .when('/classrooms/:id',{
                    controller: 'ClassroomController',
                    controllerAs: 'classroom',
                    template:'<H1>This is an inline template</H1><H1>ID值为：{{message}}</H1><div class="well"> <h2>resolveDemo</h2> {{resolveDemo}}<h4 class="alert-danger alert">resolve属性里的值会在路由成功前被预先设定好，然后注入到控制器中。通俗地将，就是等数据都“就位”后，才进行路由</h4></div>',
                    resolve: {
                        resolveDemo:function () {
                            return {
                                area:'100m2',
                                length:'20m'
                            }
                        }
                    }
                })
                .otherwise('/');/** 系统默认页 **/
        }
    ]);

    app.controller('HomeController',[ '$scope','$interval','$route','$window', HomeController]);
    app.controller('AllSchoolsController',function ($scope) {
       $scope.message='AllSchoolsController！';
    });
    app.controller('AllClassroomsController',function ($scope) {
        $scope.classroomList=[
            {'name':'一年级一班','counter':23,'code':'001'},
            {'name':'二年级一班','counter':56,'code':'001'},
            {'name':'三年级一班','counter':24,'code':'001'},
            {'name':'四年级一班','counter':33,'code':'001'}
        ];
       $scope.message='AllClassroomsController！';
    });
    app.controller('AllActivitiesController',function ($scope) {
       $scope.message='AllActivitiesController！';
    });

    function HomeController($scope,$interval,$route,$window) {
        var vm=this;
        $scope.message='这是系统首页！';
        $scope.currentRoute=$route.current
        $scope.allRoute=$route.routes
        $interval(function () {
            $scope.message=$scope.message+' '+Math.random();
        },3500);

        vm.refresh=function () {
            $route.reload();
        }

        vm.reload=function () {
            $scope.message='系统首页！';
        }
    }

    /** 路由事件 **/
    app.run(['$rootScope',function ($rootScope) {
        $rootScope.$on('$routeChangeStart',function (event,current,previous) {
            $rootScope.routeStart='event:'+event+'、current：'+current+'、previous：'+previous;
        });
        $rootScope.$on('$routeChangeSuccess',function(event, current, previous){
            var currentHtml=[];
            for(var i in current){
                currentHtml.push(i+"："+current[i]);
            }
            $rootScope.routeSuccess='event:'+event+'、current：'+current+'、previous：'+previous+"-->"+currentHtml.join("----");
        });
        $rootScope.$on('routeUpdate',function(event, current, previous){
            var currentHtml=[];
            for(var i in current){
                currentHtml.push(i+"："+current[i]);
            }
            $rootScope.routeUpdate1='event:'+event+'、current：'+current+'、previous：'+previous+"-->"+currentHtml.join("");
        });
    }]);

    /** 路由参数 **/
    app.controller('ClassroomController',['$routeParams','$scope','resolveDemo',function ($routeParams,$scope,resolveDemo) {
        $scope.resolveDemo={area:null};
        $scope.message=$routeParams.id;
        $scope.resolveDemo=resolveDemo;
    }]);
})();